package com.flyco.modules.fms.service.impl;

import com.flyco.modules.fms.entity.FmsCategory;
import com.flyco.modules.fms.entity.FmsCategorySub;
import com.flyco.modules.fms.mapper.FmsCategorySubMapper;
import com.flyco.modules.fms.mapper.FmsCategoryMapper;
import com.flyco.modules.fms.service.IFmsCategoryService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 科目大类
 * @Author: flyco
 * @Date:   2021-02-19
 * @Version: V1.0
 */
@Service
public class FmsCategoryServiceImpl extends ServiceImpl<FmsCategoryMapper, FmsCategory> implements IFmsCategoryService {

	@Autowired
	private FmsCategoryMapper fmsCategoryMapper;
	@Autowired
	private FmsCategorySubMapper fmsCategorySubMapper;
	
	@Override
	@Transactional
	public void saveMain(FmsCategory fmsCategory, List<FmsCategorySub> fmsCategorySubList) {
		fmsCategoryMapper.insert(fmsCategory);
		for(FmsCategorySub entity:fmsCategorySubList) {
			//外键设置
			entity.setCategoryId(fmsCategory.getId());
			fmsCategorySubMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(FmsCategory fmsCategory,List<FmsCategorySub> fmsCategorySubList) {
		fmsCategoryMapper.updateById(fmsCategory);
		
		//1.先删除子表数据
		fmsCategorySubMapper.deleteByMainId(fmsCategory.getId());
		
		//2.子表数据重新插入
		for(FmsCategorySub entity:fmsCategorySubList) {
			//外键设置
			entity.setCategoryId(fmsCategory.getId());
			fmsCategorySubMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		fmsCategorySubMapper.deleteByMainId(id);
		fmsCategoryMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fmsCategorySubMapper.deleteByMainId(Long.valueOf(id.toString()));
			fmsCategoryMapper.deleteById(id);
		}
	}
	
}
